/**
Copyright (c) 2013 cnova

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

library JImage requires JWidget
    struct JImage extends JWidget
        private image icon = null
        private real sizex = 128.0
        private real sizey = 128.0
        private string iconPath = null
        private integer imagetype = 2
        
        private method refresh takes nothing returns nothing
            if .iconPath != null then
                if .icon != null and GetHandleId(.icon) > 0 then
                    call DestroyImage(.icon)
                endif
                set .icon = CreateImage(.iconPath, .sizex, .sizey, 0.0, .x-.sizex/2, .y-.sizey/2, 0.0, 0, 0, 0, .imagetype)  
                if .icon != null and GetHandleId(.icon) >0 then
                    call SetImageRenderAlways(.icon, true)
                else
                    call BJDebugMsg("Error creating image: JImage.refresh(). Icon Path: " +.iconPath)
                endif
            endif
        endmethod
        method operator imageType=  takes integer t returns nothing
            if t > 0  and t < 4 then
                set .imagetype = t
                call .refresh()
            endif
        endmethod
        method operator texture= takes string path returns nothing
            set .iconPath = path
            call .refresh()
        endmethod
        method setDimensions takes real x, real y returns nothing
            set .sizex = x
            set .sizey = y
            call .refresh()
        endmethod
        stub method draw takes nothing returns nothing
            if .icon != null and GetHandleId(.icon) >0 then
                call SetImageRenderAlways(.icon, true)
                call SetImagePosition(.icon,.x-.sizex/2,.y-.sizey/2,0.0)
            endif
        endmethod
        stub method hide takes nothing returns nothing
            if .icon != null and GetHandleId(.icon) >0 then
                call SetImageRenderAlways(.icon, false)
            endif
        endmethod
        
        method onDestroy takes nothing returns nothing
            if .icon != null and GetHandleId(.icon) > 0 then
                call DestroyImage(.icon)
                set .icon = null
            endif
        endmethod
        
    endstruct
endlibrary